package com.uchengwang.ui.cm_ui.uu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.zhy.autolayout.utils.AutoUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/6/13.
 * 发布享说 图片展示
 */

public class ReleaseSpeekAdapter extends BaseAdapter{
    private Context context;
    private List<String> imageList = new ArrayList<>();

    public ReleaseSpeekAdapter(Context context){
        this.context = context;
    }

    public void setData(List<String> data){
        this.imageList = data;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public Object getItem(int i) {
        return imageList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ReleaseVH vh = null;
        if (view == null){
            vh = new ReleaseVH();
            view = LayoutInflater.from(context).inflate(R.layout.item_release_speek, null, false);

            vh.image = (ImageView) view.findViewById(R.id.itemSpeek_image);

            view.setTag(vh);
            AutoUtils.autoSize(view);
        }else {
            vh = (ReleaseVH) view.getTag();
        }
        final String url = imageList.get(i);
        File file = new File(url);
        if (file.exists()){
            //文件是否存在
            ImageManager.getInstance().loadImage(context, vh.image, url, ImageManager.ImageType.NARMOL);
        }else {
            imageList.remove(url);
        }
        return view;
    }
    
    private class ReleaseVH{
        private ImageView image;
    }

}
